package agent

import (
	"context"

	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/svc"
	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/types"
	"gitee.com/liuxuezhan/ar-platform/rpc-client/agentClient/rpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type EditRebatesFeeRateLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewEditRebatesFeeRateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *EditRebatesFeeRateLogic {
	return &EditRebatesFeeRateLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *EditRebatesFeeRateLogic) EditRebatesFeeRate(req *types.EditRebatesFeeRateReq) (resp *types.IsSuccessInfo, err error) {
	// 返佣费率--修改
	// 返佣费率管理--修改
	var _list = make([]*rpc.RebatesFeeRate, len(req.List))
	for i, v := range req.List {
		_detail := make([]*rpc.TierFeeRate, 0)
		for _, v2 := range v.Detail {
			_detail = append(_detail, &rpc.TierFeeRate{
				Type:    v2.Type,
				Name:    v2.Name,
				FeeRate: v2.FeeRate,
			})
		}
		_list[i] = &rpc.RebatesFeeRate{
			ID:        v.Id,
			Level:     v.Level,
			Tier:      v.Tier,
			Detail:    _detail,
			CreatedAt: v.CreatedAt,
			UpdatedAt: v.UpdatedAt,
		}
	}
	rpcReq := &rpc.EditRebatesFeeRateReq{
		List: _list,
	}

	report, err := l.svcCtx.AgentRPC.EditRebatesFeeRate(context.Background(), rpcReq)
	if err != nil {
		logx.Error(l.ctx, err)
		return nil, err
	}

	resp = &types.IsSuccessInfo{
		IsSuccess: report.IsSuccess,
	}

	return
}
